package cn.com.wxd.biz.website;

import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import cn.com.wxd.dao.website.CommentDao;
import cn.com.wxd.entity.website.CommentInfo;
import cn.com.wxd.common.PageInfo;
import cn.com.wxd.common.data.KeyValue;

/**
 * Title:CommentServiceImpl.java
 * Description:cn.com.wxd.biz.website
 * 
 * Company: Amumu管理平台
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-6-20 下午5:11:17
 *
 * @author WangXuDong
 * @version 1.0
 */
public class CommentServiceImpl implements CommentService {
	private CommentDao commentDao;
	private static final Logger log = LoggerFactory.getLogger(CommentServiceImpl.class);

	@Override
	@Cacheable(value = "commentCache",key="#root.methodName+#id")
	public CommentInfo getCommentById(int id) {
		return commentDao.selectCommentById(id);
	}

	@Override
	@Cacheable(value = "commentCache",key="#root.methodName+#artId+#page.pageIndex+#page.totalCount+#orderKV.key+#orderKV.value")
	public List<CommentInfo> getCommentListByArtId(int artId, PageInfo page,
			KeyValue<String, String> orderKV) {
		return commentDao.selectRootCommentByArtId(artId, page, orderKV);
	}

	@Override
	@Cacheable(value = "commentCache",key="#root.methodName+#id+#page.pageIndex+#page.totalCount+#orderKV.key+#orderKV.value")
	public List<CommentInfo> getChildComment(int id,PageInfo page,KeyValue<String,String> orderKV) {
		return commentDao.selectCommentByReid(id, page, orderKV);
	}

	@Override
	@CacheEvict(value = "commentCache",allEntries=true)
	public boolean addComment(CommentInfo comment) {
		return commentDao.insertComment(comment);
	}

	@Override
	@CacheEvict(value = "commentCache",allEntries=true)
	public boolean setDisabled(int id, boolean isDisabled) {
		return commentDao.updateDisabled(id, isDisabled);
	}

	@Override
	@CacheEvict(value = "commentCache",allEntries=true)
	public boolean removeComment(int id) {
		HashMap<Integer,Integer> praiss = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("praiss");
		HashMap<Integer,Integer> dislike = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("dislike");
		if(praiss==null){
			praiss = new HashMap<>();
		}
		if(praiss.get(id)!=null){
			praiss.remove(id);
		} //从缓存中去除
		WebsiteCache.setNoRefreshCache("praiss",praiss);  //重新加入到缓存
		if(dislike==null){
			dislike = new HashMap<>();
		}
		if(dislike.get(id)!=null){
			dislike.remove(id);
		} //从缓存中去除
		WebsiteCache.setNoRefreshCache("dislike",dislike);  //重新加入到缓存
		//设置文章的评论数量
		return commentDao.deleteComment(id);
	}
	@Override
	@CacheEvict(value = "commentCache",allEntries=true)
	public boolean batchRemoveComment(int[] ids) {
		try{
			HashMap<Integer,Integer> praiss = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("praiss");
			HashMap<Integer,Integer> dislike = (HashMap<Integer,Integer>)WebsiteCache.getNoRefreshCache("dislike");
			for (int i = 0; i < ids.length; i++) {
				if(praiss==null){
					praiss = new HashMap<>();
				}
				if(praiss.get(ids[i])!=null){
					praiss.remove(ids[i]);
				} //从缓存中去除
				if(dislike==null){
					dislike = new HashMap<>();
				}
				if(dislike.get(ids[i])!=null){
					dislike.remove(ids[i]);
				} //从缓存中去除
				commentDao.deleteComment(ids[i]);  //从数据库删除
			}
			WebsiteCache.setNoRefreshCache("praiss",praiss);  //重新加入到缓存
			WebsiteCache.setNoRefreshCache("dislike",dislike);  //重新加入到缓存
		}catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
		return true;
	}
	@Override
	@Cacheable(value = "commentCache",key="#root.methodName+#artId")
	public int selectCommCountByArtId(int artId) {
		return commentDao.selectCommCountByArtId(artId);
	}

	@Override
	@Cacheable(value = "commentCache",key="#root.methodName+#repId")
	public int selectCommCountByRepId(int repId) {
		return commentDao.selectCommCountByRepId(repId);
	}

	/////////////////////////////////////
	@Override
	@Cacheable(value = "commentCache",key="#root.methodName+#userId+#page.pageIndex+#page.totalCount+#orderKV.key+#orderKV.value")
	public List<CommentInfo> getCommentListByUserId(int userId, PageInfo page,
			KeyValue<String, String> orderKV) {
		return commentDao.selectRootCommentByUserId(userId, page, orderKV);
	}

	@Override
	@Cacheable(value = "commentCache",key="#root.methodName+#commId")
	public int getCommentReplyCount(int commId) {
		return commentDao.selectCommCountByRepId(commId);
	}
	
	public CommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	@Override
	@CacheEvict(value = "commentCache",allEntries=true)
	public boolean addDislikeToDb(int[] commentIds,int[] counts) {
		return commentDao.insertDislike(commentIds, counts);
	}

	@Override
	@CacheEvict(value = "commentCache",allEntries=true)
	public boolean addPraiseToDb(int[] commentIds,int[] counts) {
		return commentDao.insertPraise(commentIds, counts);
	}
	@Override
	@CacheEvict(value = "commentCache",allEntries=true)
	public boolean addReportToDb(int[] commentIds,int[] counts) {
		return commentDao.insertReport(commentIds, counts);
	}
	@Override
	public boolean addDislike(int commentId) {
		try {
			CommentCache.getInstance().addDislikeToCache(commentId, 1);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
		return true;
	}

	@Override
	public boolean addPraise(int commentId) {
		try {
			CommentCache.getInstance().addPraiseToCache(commentId, 1);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
		return true;
	}

	@Override
	public boolean addReport(int commentId) {
		try {
			CommentCache.getInstance().addReportToCache(commentId, 1);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return false;
		}
		return true;
	}

	@Override
	public List<CommentInfo> getComment(PageInfo page,
			KeyValue<String, String> orderKV) {
		return commentDao.selectComment(page, orderKV);
	}
}
